package com.cloudsigns.factorymanager2.project.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.adapter.PopWindowAdapter;
import com.cloudsigns.factorymanager2.engineering.customview.SpinerPopWindow;
import com.cloudsigns.factorymanager2.project.bean.SurVeyOrderBean;
import com.cloudsigns.factorymanager2.project.bean.SurveyFeedBackForBaseInfoBean;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/8/18.
 */

public class SurveyAddPrepareWorkFeedBackActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener {
    private final String TAG = SurveyAddBaseFeedBackActivity.class.getSimpleName();
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView btn_upload;
    private EditText edt_type;
    private View rl_instruction, rl_material_instruction, rl_tool_instruction;
    private EditText edt_instruction, edt_material_instruction, edt_tool_instruction;
    private ImageView img_type_right;
    private SpinerPopWindow<String> mPopWindow;
    private PopWindowAdapter mPopWindowAdapter;
    private List<String> popStrLs = new ArrayList<String>();
    private int function = -1;
    private final int YULI = 1;
    private final int MATERIAL = 2;
    private final int TOOL = 3;
    private SurVeyOrderBean surVeyOrderBean;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_survey_add_prepare_work_feedback);

        init();
        tv_title.setText("准备工作");

        if (null != getIntent()) {
            surVeyOrderBean = (SurVeyOrderBean) getIntent().getSerializableExtra(ConstantUtil.KEY_1);
        }

    }

    private void init() {
        rl_back = (RelativeLayout) findViewById(R.id.rl_back);
        tv_title = (TextView) findViewById(R.id.tv_title);

        btn_upload = (TextView) findViewById(R.id.btn_upload);
        edt_type = findViewById(R.id.edt_type);
        rl_instruction = findViewById(R.id.rl_instruction);
        rl_material_instruction = findViewById(R.id.rl_material_instruction);
        rl_tool_instruction = findViewById(R.id.rl_tool_instruction);
        edt_instruction = findViewById(R.id.edt_instruction);
        edt_material_instruction = findViewById(R.id.edt_material_instruction);
        edt_tool_instruction = findViewById(R.id.edt_tool_instruction);
        img_type_right=findViewById(R.id.img_type_right);

        mPopWindow = new SpinerPopWindow<String>(this, this);
        mPopWindowAdapter = new PopWindowAdapter(this, TAG);

        popStrLs.add("预理处理");
        popStrLs.add("材料准备");
        popStrLs.add("工具准备");

        mPopWindowAdapter.setData(popStrLs);
        mPopWindow.setAdapter(mPopWindowAdapter);

        rl_back.setOnClickListener(this);
        btn_upload.setOnClickListener(this);
        img_type_right.setOnClickListener(this);


    }

    private void updateUi() {
        if (function == YULI) {
            rl_instruction.setVisibility(View.VISIBLE);
            rl_material_instruction.setVisibility(View.GONE);
            rl_tool_instruction.setVisibility(View.GONE);
        } else if (function == MATERIAL) {
            rl_instruction.setVisibility(View.GONE);
            rl_material_instruction.setVisibility(View.VISIBLE);
            rl_tool_instruction.setVisibility(View.GONE);
        } else if (function == TOOL) {
            rl_instruction.setVisibility(View.GONE);
            rl_material_instruction.setVisibility(View.GONE);
            rl_tool_instruction.setVisibility(View.VISIBLE);
        }
    }

    private void showTypePopWindow() {
        mPopWindow.setWidth(edt_type.getWidth()/2);
        mPopWindow.showAsDropDown(edt_type);

    }

    private void submitFeedBack() {
        SurveyFeedBackForBaseInfoBean surveyFeedBackForBaseInfoBean = new SurveyFeedBackForBaseInfoBean();
        surveyFeedBackForBaseInfoBean.setCategoryDesc(edt_type.getText().toString().trim());
        if (function == YULI) {
            surveyFeedBackForBaseInfoBean.setDesc(edt_instruction.getText().toString().trim());
        } else if (function == MATERIAL) {
            surveyFeedBackForBaseInfoBean.setDesc(edt_material_instruction.getText().toString().trim());
        } else if (function == TOOL) {
            surveyFeedBackForBaseInfoBean.setDesc(edt_tool_instruction.getText().toString().trim());
        }

        surveyFeedBackForBaseInfoBean.setSurveyFunctionType(CommonEnum.SurveyFunctionType.preparework);
        Toast.makeText(this, "上传成功！", Toast.LENGTH_SHORT).show();
        Intent mIntent = new Intent();
        mIntent.putExtra(ConstantUtil.KEY_1, surveyFeedBackForBaseInfoBean);
        setResult(RESULT_OK, mIntent);
        finish();

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_upload:
                submitFeedBack();
                break;
            case R.id.rl_back:
                finish();
                break;
                // 准备内容
            case R.id.img_type_right:
                showTypePopWindow();
                break;

        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        edt_type.setText(popStrLs.get(i));
        edt_type.setSelection(popStrLs.get(i).length());

        switch (i) {
            case 0:
                function = YULI;
                updateUi();
                break;
            case 1:
                function = MATERIAL;
                updateUi();
                break;
            case 2:
                function = TOOL;
                updateUi();
                break;
        }

        if (null != mPopWindow)
            mPopWindow.dismiss();
    }
}
